//
//  PaperItemChoiceAnalysisCell.swift
//  Exam-iOS
//
//  Created by 李胜锋 on 2018/3/9.
//  Copyright © 2018年 李胜锋. All rights reserved.
//

import Foundation
import Then
import SnapKit

///选择、判断题 解析 cell
class PaperItemChoiceAnalysisCell: UITableViewCell {
    var answerLabel: UILabel!
    var myAnswerLabel: UILabel!
    var analysisLabel: UILabel!
    var startRateView: LSFStarRateView!
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public func set(answers: String?, myAnswers: String?, analysis: String?, difficulty: Int?) {
        if let answers = answers {
            answerLabel.text = "  正确答案" + answers + "  "
        }
        else {
            answerLabel.text = "  正确答案  "
        }
        
        
        
        if let myAnswers = myAnswers {
            myAnswerLabel.text = "  我的答案" + myAnswers + "  "
        }
        else {
            myAnswerLabel.text = "  我的答案  "
        }
        
        //解析
        analysisLabel.lsf_htmlParagraphTextAndFitSize(text: analysis,
                                                      width: k_screen_width - 80,
                                                      firstLineHeadIndent: 30)
        
        
        if difficulty != nil {
            startRateView.scorePercent = Float(difficulty!) / 5.0
        }
        else {
            startRateView.scorePercent = 0
        }
    }
    
    private func setup() {
        self.backgroundColor = .clear
        
        let topLine = UIView().then { (view) in
            view.backgroundColor = Config.default.grayLineViewColor
            
            contentView.addSubview(view)
            view.snp.makeConstraints({ (make) in
                make.leading.trailing.equalToSuperview()
                make.top.equalToSuperview().offset(10)
                make.height.equalTo(1)
            })
        }
        
        let answerView = UIView().then { (view) in
            view.backgroundColor = UIColor.clear
            
            contentView.addSubview(view)
            view.snp.makeConstraints({ (maker) in
                maker.centerX.equalToSuperview()
                maker.top.equalTo(topLine.snp.bottom).offset(10)
            })
        }
        answerLabel = UILabel().then({ (label) in
            label.textAlignment = .center
            label.clipsToBounds = true
            label.font = UIFont.systemFont(ofSize: 16)
            label.layer.cornerRadius = 18
            label.layer.borderWidth = 1
            label.textColor = UIColor.lsf_color16(0x19C585)
            label.layer.borderColor = label.textColor.cgColor
            
            answerView.addSubview(label)
            label.snp.makeConstraints({ (maker) in
                maker.height.equalTo(36)
                maker.width.greaterThanOrEqualTo(136)
                maker.leading.bottom.top.equalToSuperview()
            })
        })
        
        myAnswerLabel = UILabel().then({ (label) in
            label.textAlignment = .center
            label.clipsToBounds = true
            label.font = UIFont.systemFont(ofSize: 16)
            label.layer.cornerRadius = 18
            label.layer.borderWidth = 1
            label.layer.borderColor = Config.default.themeColor.cgColor
            label.textColor = Config.default.themeColor
            
            answerView.addSubview(label)
            label.snp.makeConstraints({ (maker) in
                maker.width.greaterThanOrEqualTo(136)
                maker.height.equalTo(answerLabel.snp.height)
                maker.width.equalTo(answerLabel.snp.width)
                maker.leading.equalTo(answerLabel.snp.trailing).offset(22)
                maker.top.bottom.trailing.equalToSuperview()
            })
        })
        
        let titleLabel = UILabel().then { (label) in
            label.text = "解析:"
            label.font = UIFont.systemFont(ofSize: 14)
            
            contentView.addSubview(label)
            label.snp.makeConstraints({ (maker) in
                maker.top.equalTo(answerView.snp.bottom).offset(10)
                maker.leading.equalTo(10)
                maker.height.equalTo(15)
            })
        }
        
        analysisLabel = UILabel().then({ (label) in
            label.font = UIFont.systemFont(ofSize: 14)
            label.numberOfLines = 0
            
            contentView.addSubview(label)
            label.snp.makeConstraints({ (maker) in
                maker.top.equalTo(titleLabel.snp.bottom).offset(3)
                maker.leading.equalTo(10)
                maker.trailing.equalTo(-10)
            })
        })
        
        let diffLabel = UILabel().then { (label) in
            label.text = "难度:"
            label.font = UIFont.systemFont(ofSize: 14)
            
            contentView.addSubview(label)
            label.snp.makeConstraints({ (maker) in
                maker.leading.equalTo(10)
                maker.top.equalTo(analysisLabel.snp.bottom).offset(14)
            })
        }
        
        startRateView = LSFStarRateView().then({ (view) in
            view.bounds = CGRect.init(x: 0, y: 0, width: 142, height: 22)
            view.setBottomImageName("star", topImgName: "start_fill", count: 5)
            
            contentView.addSubview(view)
            view.snp.makeConstraints({ (maker) in
                maker.size.equalTo(CGSize.init(width: 142, height: 22))
                maker.leading.equalTo(diffLabel.snp.trailing).offset(5)
                maker.centerY.equalTo(diffLabel.snp.centerY)
                maker.bottom.equalTo(-10)
            })
            
        })
    }
}
